<?php
/**
 * Ecart
 *
 * This file is part of Ecart.
 *
 * Ecart is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Ecart is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Ecart.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category    Ecart
 * @package     Ecart_Sales
 * @copyright   Copyright 2008-2009 E-Cart LLC
 * @license     GNU Public License V3.0
 */

/**
 *
 * @category    Ecart
 * @package     Ecart_Sales
 * @subpackage  Model
 * @author      Ecart Core Team <core@ecartcommerce.com>
 */
class Ecart_Sales_Model_Order_Status_Run
{

    /**
     *
     * @param string $method
     * @param Ecart_Sales_Model_Order_Row $order
     * @return mixed bool
     */
    protected function _paymentCallback($method, $order)
    {
        return Ecart_Payment::getMethod($order->payment_method_code)->$method($order);
    }
    /* @TODO Tracking, etc*/
//    protected function _shippingCallback($method, $order)
//    {
//        return Ecart_Shipping::getMethod($order->shipping_method_code)->$method($order);
//    }

    /**
     *  "new" => "pending"
     *
     * @param Ecart_Sales_Model_Order_Row $order
     * @return bool
     */
    public function pending(Ecart_Sales_Model_Order_Row $order)
    {
        if (!$this->_paymentCallback(__FUNCTION__, $order)) {
            return false;
        }
        $modelStock = Ecart::single('catalog/product_stock');
        $products = $order->getProducts();
        foreach ($products as $product) {
            if ($product['backorder']) {
                continue;
            }

            if (!$modelStock->find($product['product_id'])->current()->canPending(
                $product['quantity'],
                $product['variation_id']
            )) {

                Ecart::message()->addError(
                    Ecart::translate('sales')->__(
                        "Can't buy %s with qty %s",
                        $product['name'],
                        $product['quantity']
                ));
                return false;
            }
        }

        $modelStock = Ecart::single('catalog/product_stock');
        foreach ($products as $product) {
            if ($product['backorder']) {
                continue;
            }
            $stockRow = $modelStock->find($product['product_id'])->current();
            $quantity = $stockRow->getQuantity(
                $product['variation_id']
            );
            $stockRow->setQuantity(
                $quantity - $product['quantity'],
                $product['variation_id']
            );

        }

        return true;
    }

    /**
     *  "pending" => "processing"
     *
     * @param Ecart_Sales_Model_Order_Row $order
     * @return bool
     */
    public function processing(Ecart_Sales_Model_Order_Row $order)
    {
        if (!$this->_paymentCallback(__FUNCTION__, $order)) {
            return false;
        }
        return true;
    }

    /**
     * "processing" => "ship"
     *
     * @param Ecart_Sales_Model_Order_Row $order
     * @return bool
     */
    public function ship(Ecart_Sales_Model_Order_Row $order)
    {
        $modelStock = Ecart::single('catalog/product_stock');
        $products = $order->getProducts();
        foreach ($products as $product) {
            if (!$product['backorder']) {
                continue;
            }
            if (!$modelStock->find($product['product_id'])->current()->canShipping(
                $product['quantity'],
                $product['variation_id']
            )) {
                 Ecart::message()->addError(
                    Ecart::translate('sales')->__(
                        "Product can't be shipped. Name: %s, Sku: %s",
                        $product['name'],
                        $product['sku']
                    )
                 );
                return false;
            }
        }
        $modelStock = Ecart::single('catalog/product_stock');
        foreach ($products as $product) {
            if (!$product['backorder']) {
                continue;
            }
            $stockRow = $modelStock->find($product['product_id'])->current();
            $quantity = $stockRow->getQuantity(
                $product['variation_id']
            );
            $stockRow->setQuantity(
                $quantity - $product['quantity'],
                $product['variation_id']
            );
        }
        return true;
    }

    public function delivered(Ecart_Sales_Model_Order_Row $order)
    {
        // @todo _shippingCallback
        return true;
    }

    public function complete(Ecart_Sales_Model_Order_Row $order)
    {
        return true;
    }

    public function hold(Ecart_Sales_Model_Order_Row $order)
    {
        return true;
    }

    public function cancel(Ecart_Sales_Model_Order_Row $order)
    {
        if (!$this->_paymentCallback(__FUNCTION__, $order)) {
            return false;
        }
        $modelStock = Ecart::single('catalog/product_stock');
        foreach ($order->getProducts() as $product) {
            $stockRow = $modelStock->find($product['product_id'])->current();
            $quantity = $stockRow->getQuantity($product['variation_id']);
            $stockRow->setQuantity(
                $quantity + $product['quantity'],
                $product['variation_id']
            );
        }
        return true;
    }

    public function refund(Ecart_Sales_Model_Order_Row $order)
    {
        if (!$this->_paymentCallback(__FUNCTION__, $order)) {
            return false;
        }
        $modelStock = Ecart::single('catalog/product_stock');
        foreach ($order->getProducts() as $product) {
            $stockRow = $modelStock->find($product['product_id'])->current();
            $quantity = $stockRow->getQuantity($product['variation_id']);
            $stockRow->setQuantity(
                $quantity + $product['quantity'],
                $product['variation_id']
            );
        }
        return true;
    }

    /**
     * Must always return true
     * @param Ecart_Sales_Model_Order_Row $order
     * @return bool
     */
    public function failed(Ecart_Sales_Model_Order_Row $order)
    {
        $this->_paymentCallback(__FUNCTION__, $order);
        return true;
    }

    public function __call($call, $argv)
    {
        return in_array($call, Ecart_Collect_OrderStatus::collect());
    }
}